package com.ydgk.controller;

import com.ydgk.bean.Admin;
import com.ydgk.service.AdminService;
import com.ydgk.tool.Tool;
import com.ydgk.vo.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminControllor {

    @Autowired
    private AdminService adminService;

    private static final String UPLOAD_DIRECTORY = "upload/admin";
    public static MultipartFile fileForAface = null;

    @ModelAttribute
    public void getOrder(@RequestParam(value = "aid",required = false,defaultValue = "0")Integer aid, Map map){
        if(aid != 0){
            Admin admin = adminService.findById(aid);
            map.put("admin",admin);
        }
    }

    @ResponseBody
    @RequestMapping("/afaceUpload")
    public AjaxResult ffaceUpload(@RequestParam("file") MultipartFile file) throws IOException {
        this.fileForAface = file;
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    @RequestMapping(value = "/updateInfo")
    public String updateInfo(Admin admin , HttpSession session){
        adminService.updateById(admin);
        session.setAttribute("admin",admin);
        return "redirect:/toAdminInfo";
    }

    @RequestMapping(value = "/updateSign")
    public String updateSign(Admin admin , HttpSession session){
        adminService.updateById(admin);
        session.setAttribute("admin",admin);
        return "redirect:/toAdminInfo";
    }

    @ResponseBody
    @RequestMapping(value = "/updateFace")
    public AjaxResult updateFurnitureFace(Admin admin, HttpServletRequest request, HttpSession session) throws IOException {
        ServletContext application = request.getServletContext();
        AjaxResult ajaxResult = new AjaxResult();
        try{
            String newfilename = Tool.upload(request,fileForAface,UPLOAD_DIRECTORY );
            String aface = admin.getAface();
            admin.setAface(UPLOAD_DIRECTORY + "/" + newfilename);
            adminService.updateById(admin);
            if(aface != null){
                new File(application.getRealPath("/"+aface)).delete();
            }
            session.setAttribute("admin",admin);
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping(value = "/updateApass")
    public AjaxResult updateApass(Admin admin, HttpSession session, HttpServletResponse response){
        adminService.updateById(admin);
        Cookie cookieName = new Cookie("aname", admin.getAname());
        Cookie cookiePass = new Cookie("apass", admin.getApass());
        cookieName.setMaxAge(0);
        cookiePass.setMaxAge(0);
        cookieName.setPath("/");
        cookiePass.setPath("/");
        response.addCookie(cookieName);
        response.addCookie(cookiePass);
        session.invalidate();
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

}
